package view.controller;

import common.Message;
import common.NowUser;
import common.User;
import dao.modifypwd;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.event.ActionEvent;
import javafx.scene.control.Button;
import javafx.scene.control.PasswordField;
import javafx.stage.Stage;
import javafx.util.Duration;
import server.ClientConnServer;
import utils.MD5;
import view.FriendList;
import view.Login;

import javax.swing.*;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.concurrent.BrokenBarrierException;
import java.util.regex.Pattern;

public class ChangePswController {
    Pattern password_pattern =Pattern.compile("[^ [A-Za-z0-9]+$]{6,16}");

    public PasswordField chpsw;
    public PasswordField chagainpsw;
    public Button backbt;
    public Button changepsw;

    public void tochangepsw(ActionEvent actionEvent) throws IOException, BrokenBarrierException, InterruptedException {
        System.out.println(chagainpsw.getText());//确认密码
//        User user = new User();
//        Message msg = new Message();
//        msg.setUser(user);
        if (chpsw.getText().equals("")) {
            JOptionPane.showMessageDialog(null, "密码不能为空", "提示", JOptionPane.ERROR_MESSAGE);
        }else if(!password_pattern.matcher(chpsw.getText()).matches()){
            JOptionPane.showMessageDialog(null, "密码格式错误", "提示", JOptionPane.ERROR_MESSAGE);
        } else if (chagainpsw.getText().equals("")) {
            JOptionPane.showMessageDialog(null, "确认密码不能为空", "提示", JOptionPane.ERROR_MESSAGE);
        } else if(!chpsw.getText().equals(chagainpsw.getText())){ //两次密码不一致
            JOptionPane.showMessageDialog(null, "两次密码不一致", "提示",JOptionPane.ERROR_MESSAGE);
        } else{
            //System.out.println("s"+cpwdyz.getText());
//            Message msg1 = new Message();
//            User u=new User();
            MD5 md5 = new MD5();
//            msg1.setType("xgpwd");
//            u.setPsw(md5.generateMD5(chpsw.getText()));
//            msg1.setUser(u);
//            NowUser.changepsw = false;
//            new ClientConnServer().sendinfo(msg1);
//            NowUser.cyclicBarrier.reset();
//            NowUser.cyclicBarrier.await();
            boolean flag = new modifypwd().mpwd(NowUser.getUser().getEmail(),md5.generateMD5(chpsw.getText()));
            if(flag){
                JOptionPane.showMessageDialog(null, "修改成功", "提示",JOptionPane.PLAIN_MESSAGE);
                Stage stage = (Stage) changepsw.getScene().getWindow();
                Stage newstage = new Stage();
                stage.close();
                Socket s=new Socket("localhost",9999);
                NowUser.setSocket(s);
                NowUser.oos = new ObjectOutputStream(s.getOutputStream());
                NowUser.ois = new ObjectInputStream(s.getInputStream());
                Login rj = new Login();
                try {
                    rj.start(newstage);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }else{
                JOptionPane.showMessageDialog(null, "修改失败", "提示",JOptionPane.ERROR_MESSAGE);
            }
        }
    }

    public void tobackfriendlist(ActionEvent actionEvent) {
        Stage stage = (Stage) backbt.getScene().getWindow();
        stage.close();
//        Stage newstage=new Stage();
//        FriendList rj= new FriendList();
//        try {
//            rj.start(newstage);
////                        rj.Show();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
    }
}
